<template>
  <div id="app">
    <router-view />
    <audio id="myAudio" src="./assets/audio/dia.mp3" loop muted></audio>
  </div>
</template>

<script>
import {queryExtractCoinMsg} from '@/api/system/extractCoin'
import {requireRechargeMsg} from '@/api/system/memberRecharge'
import {queryKYCMsg} from '@/api/system/member'
import { getToken } from '@/utils/auth'
import { MessageBox } from 'element-ui'
export default  {
  name:  'App',
  data() {
    return {
      msg:{
        ExtractCoinMsg:0,
        RechargeMsg:0,
        KYCMsg:0
      },
      isPlay:false,
      timer:null,
      notify:null
    }
  },
  metaInfo() {
      return {
          title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
          titleTemplate: title => {
              return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
          }
      }
  },
  mounted() {
    this.getMsg()
    this.timer = setInterval(this.getMsg, 5000);
  },
  methods: {
    async getMsg(){
      if (!getToken()) {
        return
      }
      const ExtractCoinMsg = await queryExtractCoinMsg()
      const RechargeMsg = await requireRechargeMsg()
      const KYCMsg = await queryKYCMsg()
      this.msg.ExtractCoinMsg = ExtractCoinMsg.data.length
      this.msg.RechargeMsg = RechargeMsg.data.length
      this.msg.KYCMsg = KYCMsg.data.length
      if (this.msg.KYCMsg || this.msg.RechargeMsg || this.msg.ExtractCoinMsg) {
        this.getXX()
      }else{
        var audio = document.getElementById('myAudio');
        audio.pause()
        if (this.notify) {
          this.notify.close()
        }
        this.isPlay = false
      }
    },

    getXX(){
      if (this.isPlay) {

      } else {
        var audio = document.getElementById('myAudio');
        var button = document.createElement('button')
        button.addEventListener('click',()=>{
          audio.play().then(res=>{
            console.log('播放成功');
          }).catch(err=>{
            MessageBox.confirm('检测到当前网站在静音状态，请设置浏览器允许播放音频，也可以选择点击下方播放按钮临时授权播放，', '系统提示', {
              confirmButtonText: '播放',
              cancelButtonText: '取消',
              type: 'warning'
            }
            ).then(() => {
              audio.play()
            }).catch(() => {
            });
          }); // 播放音频
        })
        audio.muted = false;
        audio.currentTime = 0; // 从头开始播放
        button.click()
        this.isPlay = true
      }
      if (this.notify) {
        this.notify.close()
      }
      this.notify = this.$notify({
        title: '提示',
        message: `你有${this.msg.KYCMsg?'实名认证审核'+ this.msg.KYCMsg+ '条，':''}
        ${this.msg.RechargeMsg?'充币审核'+ this.msg.RechargeMsg+ '条，':''}
        ${this.msg.ExtractCoinMsg?'提币审核'+ this.msg.ExtractCoinMsg+ '条，':''}请及时处理`,
        showClose: false,
        position: 'bottom-right',
        duration: 0
      });
    }
  },
}
</script>
